﻿Public Class STRGT
    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub STRGT_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(mtrgtid AS CHAR(4)) AS MTRGTID, mtrgtname FROM masmtypergt WHERE status = '1' AND sdet = '1'"
        s = New FILTERCLASS(MTRGTTxt, sql, "รหัสหมวดคู่สัญญา,ชื่อหมวดคู่สัญญา", "50,160", "1,1", "1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True

        querySTRGTGrid()

        DesignForm(Me)
        DesignGrid(MASSTYPERGTGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Public Sub querySTRGTGrid()
        Dim objSelectDataMastypergt As New mastypergt
        Dim masstypergtData As DataTable
        masstypergtData = connect.GetTable(objSelectDataMastypergt.selectMasstypergt_)
        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
        MASSTYPERGTGrid.PrimaryGrid.DataSource = masstypergtData
    End Sub

    '---------filter right------------

    Private Sub queryFilterGrid(mtCode As String)
        Dim sql As String
        sql = "SELECT a.`strgtid` AS 'ID กลุ่ม', b.`mtrgtname` AS 'ชื่อหมวดคู่สัญญา', a.`strgtname` AS 'ชื่อกลุ่มคู่สัญญา', a.`sdet` AS 'สถานะการมีรายการย่อย', a.`status` AS 'สถานะการเปิดใช้งาน', a.`mtrgtid` AS 'ID หมวดคู่สัญญา' FROM masstypergt a JOIN masmtypergt b ON a.mtrgtid = b.mtrgtid "

        If mtCode IsNot Nothing Then
            sql += "WHERE a.`mtrgtid` = '" & mtCode & "';"
        End If

        Dim dt As DataTable
        dt = connect.GetTable(sql)
        MASSTYPERGTGrid.PrimaryGrid.DataSource = dt
    End Sub

    Private Sub filterGridChk_CheckedChanged(sender As Object, e As EventArgs) Handles mtGridChk.CheckedChanged
        If mtGridChk.Checked = True Then
            queryFilterGrid(MTRGTTxt.Tag)
        Else
            querySTRGTGrid()
        End If
    End Sub

    '---------------------------------

    Private Sub MTRGTTxt_Click(sender As Object, e As EventArgs) Handles MTRGTTxt.Click
        MTRGTTxt.Text = ""
        MTRGTTxt.Tag = ""
    End Sub

    Private Sub MASSTYPERGTGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASSTYPERGTGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        'f.TXT_CID.Text = grid.Cells.Item(2).Value
        STRGTIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ID กลุ่ม").Value()
        MTRGTTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ID หมวดคู่สัญญา").Value()
        MTRGTTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อหมวดคู่สัญญา").Value()
        STRGTNAMETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อกลุ่มคู่สัญญา").Value()

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการมีรายการย่อย").Value() = True Then
            masstypergtSDETChk.Checked = True
        Else
            masstypergtSDETChk.Checked = False
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            masstypergtStatusChk.Checked = True
        Else
            masstypergtStatusChk.Checked = False
        End If

        Dim objChkboxUpdate As New CLICKGRIDCHECKBOX
        objChkboxUpdate.clickChkboxUpdate("masstypergt", CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการมีรายการย่อย").Value(), "sdet", "strgtid", STRGTIDTxt.Text)
        objChkboxUpdate.clickChkboxUpdate("masstypergt", CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value(), "status", "strgtid", STRGTIDTxt.Text)
    End Sub

    Private Sub addMasstypergt_Click(sender As Object, e As EventArgs) Handles addMasstypergt.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If MTRGTTxt.Text.ToString.Length > 0 And STRGTNAMETxt.Text.ToString.Length > 0 Then
                    Dim objInsertData As New mastypergt
                    objInsertData.stMTRGTID_ = MTRGTTxt.Tag
                    objInsertData.STRGTNAME_ = STRGTNAMETxt.Text
                    objInsertData.SDET_ = masstypergtSDETChk.Checked
                    objInsertData.STATUS_ = masstypergtStatusChk.Checked
                    objInsertData.insertMasstypergtData()

                    Dim objSelectDataMastypergt As New mastypergt
                    Dim masstypergtData As DataTable
                    masstypergtData = connect.GetTable(objSelectDataMastypergt.selectMasstypergt_)
                    'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
                    MASSTYPERGTGrid.PrimaryGrid.DataSource = masstypergtData

                    STRGTNAMETxt.Text = ""
                    masstypergtSDETChk.Checked = False
                    masstypergtStatusChk.Checked = False
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editMasstypergt_Click(sender As Object, e As EventArgs) Handles editMasstypergt.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If MTRGTTxt.Text.ToString.Length > 0 And STRGTNAMETxt.Text.ToString.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลกลุ่มคู่สัญญาดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                    If editStat = 1 Then
                        Dim objEditData As New mastypergt
                        objEditData.STRGTID_ = STRGTIDTxt.Text
                        objEditData.stMTRGTID_ = MTRGTTxt.Tag
                        objEditData.STRGTNAME_ = STRGTNAMETxt.Text
                        objEditData.SDET_ = masstypergtSDETChk.Checked
                        objEditData.STATUS_ = masstypergtStatusChk.Checked
                        objEditData.editMasstypergtData()

                        Dim objSelectDataMastypergt As New mastypergt
                        Dim masstypergtData As DataTable
                        masstypergtData = connect.GetTable(objSelectDataMastypergt.selectMasstypergt_)
                        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
                        MASSTYPERGTGrid.PrimaryGrid.DataSource = masstypergtData

                        STRGTNAMETxt.Text = ""
                        masstypergtSDETChk.Checked = False
                        masstypergtStatusChk.Checked = False
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL(Masstypergt.Text)
        nextform.ShowDialog()
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles ButtonItem1.Click
        Dim nextform As New IMPORTEXCEL(Masstypergt.Text)
        nextform.ShowDialog()
    End Sub
End Class